/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import easybooking.data.Booking;
import easybooking.data.Customer;
import easybooking.data.Flight;
import easybooking.data.Seat;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author tim
 */
public class BookingTableModel extends AbstractTableModel {

    private Vector<Booking> bookings;
     private EBInterfaceClient ebic;
    private static final int ID_INDEX = 0;
    private static final int DATE_INDEX = 1;
    private static final int FLIGHT_INDEX = 2;
    private static final int CUSTOMER_INDEX = 3;
    private static final int SEAT_INDEX = 4;
    private static final int CHECKINDATE_INDEX = 5;
    private static final int CHECKEDIN_INDEX = 6;
    private static final int OBJECT = 7;
    private String[] columnNames = {"ID", "Date", "Flight", "Customer", "Seat", "Check in Date"};


    public BookingTableModel(Vector<Booking> f, EBInterfaceClient ebifc) {
        this.ebic = ebifc;
        bookings = f;
    }
/**
 * get the number of rows the table needs
 * @return the number of booking objects in the bookings Vector
 */
    public int getRowCount() {
        return bookings.size();
    }
/**
 * get the number of columns
 * @return the number of the column Names
 */
    public int getColumnCount() {
        return columnNames.length;

    }

    @Override
            /**
     * get the name of a column
     * @param col is the number of the column which name is wanted
     * @return the name of a column
     */
    public String getColumnName(int col) {
        return columnNames[col];
    }
/**
 * prepares the airline data for the table
 * @param rowIndex the row in which the data get insert
 * @param columnIndex the column in which the data get insert
 * @return the prepared object
 */
    public Object getValueAt(int rowIndex, int columnIndex) {
        try {
            Booking b = (Booking) bookings.get(rowIndex);
            switch (columnIndex) {
                case ID_INDEX:
                    return b.getBookingId();
                case DATE_INDEX:
                   return b.getDateTime();
                case FLIGHT_INDEX:
                    Flight f = ebic.findFlightByKey(b.getFlightId());;
                    return "Flight No: " + f.getFlightId() + " From: " + ebic.findAirportByKey(f.getStartAirportId()).getCity() + " To: "+ ebic.findAirportByKey(f.getDestAirportId()).getCity();
                case CUSTOMER_INDEX:
                    Customer c = ebic.findCustomerByKey(b.getCustomerId());
                    return c.getFirstname()+ " "+ c.getLastname();
                case SEAT_INDEX:
                    Seat s= ebic.findSeatByKey(b.getSeatId());
                    return String.valueOf(s.getRow())+String.valueOf(s.getSeat());
                case CHECKINDATE_INDEX:
                    if (b.getIsCheckedIn()){
                        return b.getCheckedInDateTime();
                    }
                    else return "-";
                case OBJECT:
                    return b;
                default:
                    return new Object();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(BookingTableModel.class.getName()).log(Level.SEVERE, null, ex);
            return new Object();
        }
    }
}

